<?php

function FSubstr($string,$start,$length,$symbol='…',$code='utf-8'){
	$sy = $code=='utf-8' ? 3 : 2;
	$chars = $string;   
	$i=0;   
	while($k < $length){   
		if (preg_match ("/[0-9a-zA-Z]/", $chars[$i])){  
			$m++;   
		} else {  
			$n++;
		}
		$k = $n/$sy+$m/2;   
		$l = $n/$sy+$m; 
		$i++;
	} 
	$str1 = iconv_substr($string,$start,$l,$code);
	if((strlen($string)-$m-$n) > 0) $str1 .= $symbol;
	return $str1;   
}  

function NewUrl($Dialog,$Text,$Url){
	if ($Url == '1') {$Url = $_SERVER['REQUEST_URI'];} else if ($Url == '0'){$Url = $_SERVER['SCRIPT_NAME'];}
	echo '<script language="javascript">';
	if ($Dialog == 1){echo 'alert("' . $Text . '");';}
	if ($Url == '-1'){echo 'history.back(-1);';} else {echo 'location="' . $Url . '";';}
	echo '</script>';
	exit;
}

function matchtime($MatchID){
	global $db,$dbprefix;
	if (!empty($MatchID)){
		$sql = "SELECT mt_start_date,mt_end_date FROM `match` WHERE `mt_id` = '".$MatchID."'";
		$res = $db->SelectSQL($sql);
		$num = count($res);
		if ($num>0){
			$arr['start_time'] = $res[0][0];
			$arr['end_time'] = $res[0][1];
		}
	}
	return $arr;
}



//获取当前比赛的信息数据  add by zhangkai 2015.4.24
function selMatchInfo($mt_id){
	global $db,$dbprefix;
	$sql_sel = "SELECT * FROM `".$dbprefix."match` WHERE mt_id = '".$mt_id."'";
	$res_sel = $db->SelectSQL($sql_sel);
	$num_sel = count($res_sel);
	if ($num_sel>0){
		return $res_sel[0];
	} else {
		return false;
	}
}


//查询分组组名
function selGroupName($mt_id){
	global $db,$dbprefix;
	$sql_sel = "SELECT id,name FROM `".$dbprefix."gp_manage` WHERE mt_id = '".$mt_id."'";
	$res_sel = $db->SelectSQL($sql_sel);
	$num_sel = count($res_sel);
	if ($num_sel>0){
		foreach ($res_sel as $i => $n){
			$group[$i]['id'] = $n['id'];
			$group[$i]['name'] = $n['name'];
		}
	}
	return $group;
}


//数字大小写转换函数
function numericDx($str){
	$numArr = array("零","一","二","三","四","五","六","七","八","九","十");
	return $numArr[$str];
}

//获取该场比赛的标准杆
function getPar($mt_id){
	global $db,$dbprefix;
	$sql = "SELECT mh_id,mh_par FROM ".$dbprefix."match_hole WHERE mh_match = '".$mt_id."' GROUP BY mh_ho_id ORDER BY mh_id ASC limit 0,18";
	$res = $db->SelectSQL($sql);
	$num = count($res);
	if ($num>0){
		foreach ($res as $i => $n){
			$arr['hole'][] = $n[0];
			$arr["hole_id"][] = $n[0];
			$arr["par"][] = $n[1];
			if ($n[0]<10) $arr['OUT'] += $n[1];
			if ($n[0]>9) $arr['IN'] += $n[1];
		}
		$arr["count"] = $num;
		return $arr;
	} else {
		return false;
	}
}


//参赛球员的排名显示
function playerSummary($mt_id,$rd_id,$mp_class_id = ''){
	global $db,$dbprefix,$maxRound;
	$sql = "SELECT mp_player_id AS mp_pl_id
				,mp_sts,mp_notes,mp_class
				,su_score,su_to_par,su_rank
				,su_rank_code,su_sts
				,su_new_total
				,su_hdcp,su_net
				,CASE WHEN su_rank is null THEN 0 ELSE 1 END AS is_null 
				,gm.`name`
		        ,gm.id
			FROM ".$dbprefix."match_player LEFT JOIN ".$dbprefix."player_summary 
			ON mp_player_id = su_pl_id AND mp_match = su_mt_id AND su_rd_id = '".$rd_id."' 
			LEFT JOIN ".$dbprefix."gp_manage gm ON gm.id = mp_class
			";	
	$sql .=" WHERE mp_match = '".$mt_id."'  ";	
	if ($mp_class_id){
		$sql .= " AND mp_class = '".$mp_class_id."' ";
	}
	/*if($rd_id == 1){
		$sql .= "AND mp_notes <> 'R1' ";
	} else if ($rd_id == 2){
		$sql .= "AND mp_notes <> 'R1' AND mp_notes <> 'R2' ";
	} else if ($rd_id == 3){
		$sql .= "AND mp_notes not in ('R1','R2','R3') ";
	} else {
		$sql .= "AND mp_notes not in ('R1','R2','R3','R4') ";
	}*/
	$sql .= "ORDER BY  ";
	$sql .= "mp_class ASC,is_null DESC, ";
	$sql .= "su_rank,su_to_par, su_pl_id ";

	$res = $db->SelectSQL($sql);
	$num = count($res);
	if ($num>0){
		foreach ($res as $i => $n){
			if($n['mp_sts'] != '' and $n['mp_notes']=='R'.$rd_id){
				$arr['rank'][$n['mp_pl_id']] = $n['mp_sts'];
			} else {
				if($n['su_rank_code'] == ''){
					$arr['rank'][$n['mp_pl_id']] = $n['su_rank'];
				} else if ($n['su_rank_code'] == 'T'){
					$arr['rank'][$n['mp_pl_id']] = 'T'.$n['su_rank'];
				} else {
					$arr['rank'][$n['mp_pl_id']] = $n['mp_sts'];
				}
			}
			$arr['checked'][$n['mp_pl_id']] = $n['su_sts'];
			#$arr['nnp'][$n['mp_pl_id']] = $n['su_new_total'];
			#$arr['hdcp'][$n['mp_pl_id']] = $n['su_hdcp'];
			#$arr['net'][$n['mp_pl_id']] = $n['su_net'];
			if ($n['su_rank'] == 1 && $rd_id == $maxRound) $arr['notes'][$n['mp_pl_id']] = $n['mp_notes'];
		}
	}
	$su_sql = " SELECT mp_player_id AS mp_pl_id, mp_sts, mp_notes, su_score, su_to_par, su_rank, su_rank_code, su_sts,su_new_total,su_hdcp,su_net  
				FROM ".$dbprefix."match_player LEFT JOIN ".$dbprefix."player_summary 
				ON mp_player_id = su_pl_id AND mp_match = su_mt_id AND su_rd_id = '".$rd_id."' 
				WHERE mp_match = '".$mt_id."'  ";
	if ($mp_class_id){
		$su_sql .= " AND mp_class = '".$mp_class_id."' ";
	}
				#echo $sql;
	/*if($rd_id == 1){
		$su_sql .= "AND mp_notes = 'R1' ";
	} else if ($rd_id == 2){
		$su_sql .= "AND mp_notes = 'R2' ";
	} else if ($rd_id == 3){
		$su_sql .= "AND mp_notes = 'R3' ";
	} else if ($rd_id == 4){
		$su_sql .= "AND mp_notes = 'R4' ";
	}*/
	$su_sql .= "ORDER BY mp_sts,su_rank,su_to_par, su_pl_id ";
	$su_res = $db->SelectSQL($su_sql);
	$su_num = count($su_res);
	if ($su_num>0){
		foreach ($su_res as $i => $n){
			if($n['mp_sts'] != '' and $n['mp_notes']=='R'.$rd_id){
				$arr['rank'][$n['mp_pl_id']] = $n['mp_sts'];
			} else {
				if($n['su_rank_code'] == ''){
					$arr['rank'][$n['mp_pl_id']] = $n['su_rank'];
				} else if ($n['su_rank_code'] == 'T'){
					$arr['rank'][$n['mp_pl_id']] = 'T'.$n['su_rank'];
				} else{
					$arr['rank'][$n['mp_pl_id']] = $n['mp_sts'];
				}
			}
			$arr['checked'][$n['mp_pl_id']] = $n['su_sts'];
			#$arr['nnp'][$n['mp_pl_id']] = $n['su_new_total'];
			#$arr['hdcp'][$n['mp_pl_id']] = $n['su_hdcp'];
			#$arr['net'][$n['mp_pl_id']] = $n['su_net'];
			if ($n['su_rank'] == 1 && $rd_id == $maxRound) $arr['notes'][$n['mp_pl_id']] = $n['mp_notes'];
		}
	}
	return $arr;
	
}	


//读取上一轮的排名
function playerPrevSummary($mt_id,$rd_id){
	global $db,$dbprefix,$maxRound;
	if (!empty($mt_id) && $rd_id>1){
		$round_preId = $rd_id-1;
		$sql = "SELECT mp_player_id AS mp_pl_id,su_rank,su_rank_code,mp_sts,mp_notes,su_new_total,su_hdcp,su_net,CASE WHEN su_rank is null THEN 0 ELSE 1 END AS  is_null
				FROM ".$dbprefix."match_player LEFT JOIN ".$dbprefix."player_summary 
				ON mp_player_id = su_pl_id AND mp_match = su_mt_id AND su_rd_id = '".$round_preId."'
				WHERE mp_match = '".$mt_id."' AND mp_sts='' ";		
		if($round_preId == 1) {
			$sql .= "AND mp_notes <> 'R1' ";
		}
		if($round_preId == 2) {
			$sql .= "AND mp_notes <> 'R1' AND mp_notes <> 'R2' ";
		}
		if($round_preId == 3) {
			$sql .= "AND mp_notes not in ('R1','R2','R3') ";
		}
		$sql .= "ORDER BY  is_null DESC, su_rank, su_pl_id ";
		$res = $db->SelectSQL($sql);
		$num = count($res);
		if ($num>0){
			foreach ($res as $i => $n){
				if($n['mp_sts'] != '' and $n['mp_notes']=='R'.$rd_id){
					$arr['rank'][$n['mp_pl_id']] = $n['mp_sts'];
				} else {
					if($n['su_rank_code'] == ''){
						$arr['rank'][$n['mp_pl_id']] = $n['su_rank'];
					} else if ($n['su_rank_code'] == 'T'){
						$arr['rank'][$n['mp_pl_id']] = 'T'.$n['su_rank'];
					} else {
						$arr['rank'][$n['mp_pl_id']] = $n['mp_sts'];
					}
				}
				@$arr['checked'][$n['mp_pl_id']] = $n['su_sts'];
				#$arr['nnp'][$n['mp_pl_id']] = $n['su_new_total'];
				#$arr['hdcp'][$n['mp_pl_id']] = $n['su_hdcp'];
				#$arr['net'][$n['mp_pl_id']] = $n['su_net'];
				if ($n['su_rank'] == 1 && $rd_id == $maxRound) $arr['notes'][$n['mp_pl_id']] = $n['mp_notes'];
			}
		}
	}
	return $arr;
}


//参赛球员信息
function matchPlayer($mt_id){
	global $db,$dbprefix;
	$sql = "SELECT mp_player_id,pl_cn_name,pl_en_name,pl_country,pl_type,mp_class,mp_pkclass,pl_tw_name FROM ".$dbprefix."match_player,".$dbprefix."player 
				WHERE mp_match = '".$mt_id."' AND mp_player_id = pl_id ";
	$res = $db->SelectSQL($sql);
	$num = count($res);
	if ($num>0){
		foreach ($res as $i => $n){
			$arr['playerClass'][$n[0]] = $n['mp_class'];
			$arr['playerName'][$n[0]] = $n['pl_cn_name'];
			$arr['playerEnName'][$n[0]] = $n[2];
			$arr['playerType'][$n[0]] = $n[4];
			$arr['playerCountry'][$n[0]] = empty($n[3]) ? 'CHN' : $n[3];
			$arr['playerZM'][$n[0]] = strtoupper(substr($n[2],0,1));
		}
	}
	return $arr;
}

//开始球洞
function startHole($mt_id,$rd_id){
	global $db,$dbprefix;
	if (!empty($mt_id) && !empty($rd_id)){
		$sql = "SELECT gpd_player, gp_start_hole FROM `".$dbprefix."group`, `".$dbprefix."group_detail` 
				WHERE gp_id = gpd_id AND gp_mt_id = '".$mt_id."' AND gp_rd_id = '".$rd_id."' ";
		$res = $db->SelectSQL($sql);
		$num = count($res);
		if ($num>0){
			foreach ($res as $i => $n){
				$arr[$n[0]] = $n[1];
			}
		}
	}	
	return $arr;		
}


//读取18洞成绩和topar
function getHoleScore($mt_id,$rd_id){
	global $db,$dbprefix;
	$sql = "SELECT sc_score,sc_to_par,sc_ho_id,sc_pl_id FROM ".$dbprefix."player_score 
			WHERE sc_mt_id = '".$mt_id."' AND sc_rd_id = '".$rd_id."' AND sc_type = 'M' AND sc_sts!='R' 
			AND sc_pl_id IN(SELECT mp_player_id FROM ".$dbprefix."match_player WHERE mp_match = '".$mt_id."') 
			ORDER BY sc_pl_id, sc_ho_id";
	$res = $db->SelectSQL($sql);
	$num = count($res);
	if ($num>0){
		foreach ($res as $i => $n){
			$arr['score'][$n[3]][$n[2]] = $n[0];
			$arr['topar'][$n[3]][$n[2]] = $n[1];
			if ($n[2]<10) {
				$arr['OUT'][$n[3]] += $n[0];
				$arr['OUTNUM'][$rd_id][$n[3]]++;
			}
			if ($n[2]>9) {
				$arr['IN'][$n[3]] += $n[0];
				$arr['INNUM'][$rd_id][$n[3]]++;
			}
			$arr['thru'][$n[3]]++;
		}
	}
	return $arr;
}


//读取比赛成绩
function getScore($mt_id){
	global $db,$dbprefix;
	$sql = "SELECT su_pl_id, su_rd_id, su_score, su_to_par FROM ".$dbprefix."player_summary WHERE su_mt_id = '".$mt_id."' ORDER BY su_pl_id, su_rd_id ";
	$res = $db->SelectSQL($sql);
	$num = count($res);
	if ($num>0){
		foreach ($res as $i => $n){
			$arr['rd_score'][$n['su_pl_id']][$n['su_rd_id']] = $n['su_score'];
			$arr['to_par'][$n['su_pl_id']][$n['su_rd_id']] = $n['su_to_par'];
			$arr['total_score'][$n['su_pl_id']] += $n['su_score'];
		}
	}
	return $arr;
}


function toparcolor($score){
	if (!empty($score)){
		if ($score<='-2'){
            $color = 'eagle';
        } else if ($score=='-1'){
            $color = 'birdie';
        } else if ($score=='0'){
            $color = 'par';
        } else if ($score=='1'){
            $color = 'bogey';
        } else if ($score>='2'){
            $color = 'db_bogey';
        } else {
            $color = 'par';
        }
	} else {
		$color = 'par';
	}
	return $color;
}

function colorHole($score,$par){
    if (!empty($score)){
        $topar = $score-$par;
        if ($topar<='-2'){
            $color = 'eagle';
        } else if ($topar=='-1'){
            $color = 'birdie';
        } else if ($topar=='0'){
            $color = 'par';
        } else if ($topar=='1'){
            $color = 'bogey';
        } else if ($topar>='2'){
            $color = 'db_bogey';
        } else {
            $color = 'par';
        }
    } else {
        $color = 'par';
    }
    return $color;
}

function arrayContact($arr1,$arr2){
	foreach ($arr1 as $key => $val){
		$new_arr[$key] = $val;
	}

	foreach ($arr2 as $key => $val){
		$new_arr[$key] = $val;
	}
	return $new_arr;
}

//栏目类别二维数组重新排序

function TeamArraySort($arr,$keys,$type='asc'){  
	$keysvalue = $new_array = array(); 
	if (!empty($arr)){
		foreach ($arr as $a=>$b){
			if (empty($b[$keys])){
				$karr[$a] = $arr[$a];
			} else {
				$yarr[$a] = $arr[$a];
			}
		}
		if (!empty($yarr)){
			foreach ($yarr as $k=>$v){ 
				$keysvalue[$k] = $v[$keys]; 
			}
		}

		if($type == 'asc'){ 
			#asort($keysvalue,SORT_NUMERIC); 
			foreach($keysvalue as $key=>$value){
			    $val = str_replace("T","",$value);
				$newkeysvalue[$key] = $val;
			}
            asort($newkeysvalue);
			$times = array_count_values($keysvalue);
			foreach($newkeysvalue as $k=>$v){
			    if($times[$v]>1){
				    $turnval[$k] = 'T'.$v;
				}else{
				    $turnval[$k] = $v;
				}
			}
		}else{ 
			arsort($keysvalue); 
            $turnval = $keysvalue;
		} 
		reset($turnval); 

		foreach ($turnval as $k=>$v){ 
			$new_array[$k] = $yarr[$k]; 
		} 

		if (empty($karr)){
			return $new_array;  
		} else {
			foreach ($karr as $c=>$d){ 
				$new_array[$c] = $karr[$c];
			}
			return $new_array; 
		}
	} else {
		return false;
	}
}
?>